'use strict';

webappApp.service('spotService', ['$resource', 'webRoot', function($resource, webRoot) {
  var spotResource = $resource(webRoot + '/spot/:id'),
      photoResource = $resource(webRoot + '/photo/:id'),
      self = this;

  this.results = {
    spots: []
  };

  this.get = function (id, onsuccess, onerror) {
    return spotResource.get({
      id: id
    }, onsuccess, onerror);
  };

  this.query = function (query, onsuccess, onerror) {
    spotResource.get(query, function (envelope) {
      if (onsuccess) {
        self.results.spots = envelope.results || [];
        onsuccess(envelope);
      }
    }, function (reason) {
      if (onerror) {
        self.results.spots = [];
        onerror(reason);
      }
    });
  };

  this.create = function (spot, onsuccess, onerror) {
    return spotResource.save({
      name: spot.name,
      description: spot.description,
      type: spot.type,
      location: spot.location,
      address: spot.address
    }, onsuccess, onerror);
  };
}]);
